package com.thinker.checkorder.controller;

import com.thinker.checkorder.bean.RepeatParam;
import com.thinker.checkorder.bean.ZyyJSONResult;
import com.thinker.checkorder.mailutil.SendMailUtil;
import com.thinker.checkorder.service.RepeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * RepeatController
 *
 * @author zhougf
 * @date 2019/9/16
 */
@RestController
@RequestMapping("repeat")
public class RepeatController {

    private DateTimeFormatter timeType = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private RepeatService repeatService;

    private static final DateTimeFormatter DAY = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @RequestMapping("/listRepeat")
    public ZyyJSONResult queryCustom() throws Exception{
        List<String> listOrderNo = new ArrayList<>(32);
        List<RepeatParam> listOrder = repeatService.listRepeatParam(DAY.format(LocalDateTime.now()));
        List<RepeatParam> repeatParamList = listOrder.stream().filter(repeatParam -> !(repeatParam.getOrderCount().equals(1)))
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(repeatParamList) && repeatParamList.size() != 0) {
            for (RepeatParam repeatParam : repeatParamList) {
                listOrderNo.add(repeatParam.getOrderNo());
            }
            SendMailUtil.sendMail(listOrderNo.toString());
        }
        return ZyyJSONResult.ok("xx");
    }

    @RequestMapping("/add/{id}")
    public ZyyJSONResult add(@PathVariable Long id) {
//        LocalDateTime nowTime = LocalDateTime.now();
//        String sendTime = nowTime.format(timeType);
//        System.out.println("开始时间：" + sendTime);
        for(int j = 100; j >= 1; j--) {
            repeatService.add(j);
        }
//        LocalDateTime nowTime1 = LocalDateTime.now();
//        String endTime = nowTime.format(timeType);
//        System.out.println("结束时间：" + endTime);
//        Duration duration = Duration.between(nowTime, nowTime1);
//        System.out.println("用时：" + duration.toMillis());
        return ZyyJSONResult.ok("ok");
    }
}
